package com.totcy.atom.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.github.chrisbanes.photoview.PhotoView;
import com.totcy.atom.R;

import java.util.List;

/**
 * Description 大图左右滑动适配器
 * @author : tu
 * Date: 2017-04-13
 * Time: 14:25
 */

public class PictureBrowsingAdapter extends PagerAdapter {

    private Context mContext;
    private List<String> datas;
    private View.OnClickListener onClickListener;

    public PictureBrowsingAdapter(Context context, List<String> datas,View.OnClickListener onClickListener) {
        mContext = context;
        this.datas = datas;
        this.onClickListener = onClickListener;
    }

    /**
     * Return the number of views available.
     */
    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        String url = datas.get(position);

        View contentView = LayoutInflater.from(mContext).inflate(R.layout.item_picture_browsing,null);
        PhotoView photoView = contentView.findViewById(R.id.pv_pictureBrowsing);
        photoView.setOnClickListener(onClickListener);
        final ProgressBar pb = contentView.findViewById(R.id.progressBar_loading);
        pb.setVisibility(View.VISIBLE);
        Glide.with(mContext)
                .load(url)
                //过渡动画
                .transition(DrawableTransitionOptions.withCrossFade())
               // .apply(new RequestOptions()
               //         .placeholder(R.drawable.img_default))
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        pb.setVisibility(View.GONE);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        pb.setVisibility(View.GONE);
                        return false;
                    }
                })
                .into(photoView);

        container.addView(contentView);
        return contentView;
    }
}
